//
//  RegisterViewController.m
//  YunGou
//
//  Created by macbook on 16/5/18.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "RegisterViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "NSString+Regexp.h"

@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *telText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *passwordAgainText;
@property (weak, nonatomic) IBOutlet UIButton *gainCodeBtn;

@property (assign,nonatomic) long content;
@property (nonatomic,copy) NSString *contentText;
@property (nonatomic,strong) NSDate *startDate;
@property (strong,nonatomic) NSTimer *timer;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:tap];
}

- (void)hideKeyBoard{
    [self.view endEditing:YES];
}


- (IBAction)gainCodeClicked:(id)sender {
    if (self.telText.text.length == 0) {
        [MBProgressHUD showError:@"号码不能为空"];
    }else{
        BOOL isPhoneNum = [NSString isMobileNumber:self.telText.text];
        if (isPhoneNum) {
            self.gainCodeBtn.enabled = NO;
            self.content = (arc4random()%1000000) + 100000;
            self.contentText = [NSString stringWithFormat:@"【PK夺宝】您的注册验证码为:%ld",self.content];
            [NetWorkTool sendVerfiWithPhone:self.telText.text content:self.contentText success:^(id object) {
                [MBProgressHUD showSuccess:@"发送成功"];
            } failure:^(NSError *error) {
                [MBProgressHUD showError:@"发送失败"];
            }];
            //定时器
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
            _startDate = [NSDate date];
            [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
        }
    }
}

//辅助函数
- (void)updateTime:(NSTimer*)sender
{
    double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    int second = 60 - (int)(deltaTime + 0.5);
    if (second < 0.0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        self.gainCodeBtn.enabled = YES;
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateDisabled];
    }else{
        NSString *title = [NSString stringWithFormat:@"%d秒",second];
        [self.gainCodeBtn setTitle:title forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:title forState:UIControlStateDisabled];
    }
}

- (IBAction)registerClicked:(id)sender {
    NSString *telText = _telText.text;
    NSString *codeText = _codeText.text;
    NSString *pswText = _passwordText.text;
    NSString *pswTextAgain = _passwordAgainText.text;
    if (telText.length != 0) {
        if ([NSString isMobileNumber:telText]) {
            if ([codeText isEqualToString:[NSString stringWithFormat:@"%ld",_content]]) {
                if (pswText.length != 0) {
                    if ([pswText isEqualToString:pswTextAgain]) {
                        [NetWorkTool registerWithPhone:telText password:pswText success:^(id object) {
                            if ([object[@"code"] integerValue] == 1) {
                                [MBProgressHUD showSuccess:@"注册成功"];
                            }else{
                                [MBProgressHUD showError:@"注册失败"];
                            }
                        } failure:^(NSError *error) {
                            
                        }];
                    }else{
                        [MBProgressHUD showError:@"两次密码输入不一致"];
                    }
                }else{
                    [MBProgressHUD showError:@"密码不能为空"];
                }
            }else{
                [MBProgressHUD showError:@"验证码不正确"];
            }
        }else{
            [MBProgressHUD showError:@"号码格式不正确"];
        }
    }else{
        [MBProgressHUD showError:@"号码不能为空"];
    }
}


//返回登录界面
- (IBAction)backToLogin:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField returnKeyType] != UIReturnKeyDone) {
        NSInteger nextTag = textField.tag + 1;
        UITextField *nextTextField = [self.view viewWithTag:nextTag];
        [nextTextField becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
